package modelLayer;
import java.util.*;

public class OrderContainer {
    private static OrderContainer instance;
    private ArrayList<Order> orders;
    private int nextId;
    
    private OrderContainer(){
        orders = new ArrayList<>();
   }
   
    public static OrderContainer getInstance(){
        if(instance == null){
            instance = new OrderContainer();
        }
        return instance;
   }
   
    public void addOrder(Order ord){
        this.nextId++;
        ord.setId(this.nextId);
        orders.add(ord);
   }
   
   public Order findOrder(int id){
        boolean found=false;
        int index=0;
        System.out.println(orders.size());
        while(index < orders.size() && !found) {
            Order ord = orders.get(index);
            if(ord.getId() == id){
                found=true;
            }
            else {
                index++;            
            }
        }
        if(found){
            return orders.get(index);
        }
        else{
            return null;
        }
   }
   
   public int numberOfOrders() {
       return orders.size();
   }
   
   public ArrayList<Order> ordersByEmployee(int cpr) {
       ArrayList<Order> empOrds = new ArrayList<>();
       for (Order ord : orders) {
           Employee emp = ord.getEmployee();
           if(emp.getCpr() == cpr && !ord.getStatus().equals("cancelled") ) {
               empOrds.add(ord);
           }
       }
       return empOrds;
    }
    
   public ArrayList<Order> ordersByMonth(String month) {
       ArrayList<Order> monthOrds = new ArrayList<>();
       for (Order ord : orders) {
           if(ord.getDate().substring(3,5).equals(month) && !ord.getStatus().equals("cancelled")) {
               monthOrds.add(ord);
           }
       }
       return monthOrds;
    } 
}